#include	<sys/wait.h>
#include	<apue.h>

#define	PAGER	"${PAGER:-more}" /* environment variable, or default */

int
main(int argc, char *argv[])
{
	char	line[MAXLINE];
	FILE	*fpin, *fpout;

	if (argc != 2)
		printf("usage: a.out <pathname>");
	if ( (fpin = fopen(argv[1], "r")) == NULL)
		printf("can't open %s", argv[1]);
	printf("%s\n", PAGER);

	if ( (fpout = popen(argv[2], "w")) == NULL)
		printf("popen error");

	printf("%d\n", fpout);
		/* copy argv[1] to pager */
	while (fgets(line, MAXLINE, fpin) != NULL) {
		if (fputs(line, fpout) == EOF)
			printf("fputs error to pipe");
	}
	if (ferror(fpin))
		printf("fgets error");
	if (pclose(fpout) == -1)
		printf("pclose error");
	exit(0);
}
